package com.cnpc.iot.video.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.cnpc.framework.base.dao.RedisDao;
import com.cnpc.framework.utils.HttpClientUtil;
import com.cnpc.iot.video.entity.AccessTokenResponse;
import com.cnpc.iot.video.service.VideoService;

@Service
public class VideoServiceImpl implements VideoService {
	public static final String ACCESS_TIKEN = "https://open.ys7.com/api/lapp/token/get";
	public static final String START_YUNTAI_CON = "https://open.ys7.com/api/lapp/device/ptz/start";
	public static final String STOP_YUNTAI_CON = "https://open.ys7.com/api/lapp/device/ptz/stop";
	private static final String REDIS_ACCESS_TOKEN = "VIDEO_ACC_TOKEN:";
	@Autowired
	private RedisDao redisDao;

	@Override
	public String getAccessToken(String appKey, String appSecret) {
		String accessToken = redisDao.get(REDIS_ACCESS_TOKEN + appKey);
		if (StringUtils.isEmpty(accessToken)) {
			Map<String, String> param = new HashMap<>();
			param.put("appKey", appKey);
			param.put("appSecret", appSecret);
			String result = HttpClientUtil.doPost(ACCESS_TIKEN, param);
			AccessTokenResponse re = JSONObject.parseObject(result, AccessTokenResponse.class);
			if (null != re && re.getCode().equals("200")) {
				String token = (String) re.getData().get("accessToken");
				long expireTime = (long) re.getData().get("expireTime");
				if (!StringUtils.isEmpty(token)) {
					redisDao.add(REDIS_ACCESS_TOKEN + appKey, token);
					redisDao.expire(REDIS_ACCESS_TOKEN + appKey, 6, TimeUnit.DAYS);
				}
				return token;
			}
		} else {
			return accessToken;
		}

		return null;
	}

	@Override
	public boolean startYunTaiCont(String accessToken, String deviceSerial, int channelNo, int direction, int speed) {
		Map<String, String> param = new HashMap<>();
		param.put("accessToken", accessToken);
		param.put("deviceSerial", deviceSerial);
		param.put("channelNo", String.valueOf(channelNo));
		param.put("direction", String.valueOf(direction));
		param.put("speed", String.valueOf(speed));
		String result = HttpClientUtil.doPost(START_YUNTAI_CON, param);
		Map<String, String> map = JSONObject.parseObject(result, Map.class);
		if (map.get("code").equals("200")) {
			return true;
		}
		return false;
	}

	@Override
	public boolean stopYunTaiCont(String accessToken, String deviceSerial, int channelNo, int direction) {
		Map<String, String> param = new HashMap<>();
		param.put("accessToken", accessToken);
		param.put("deviceSerial", deviceSerial);
		param.put("channelNo", String.valueOf(channelNo));
		param.put("direction", String.valueOf(direction));
		String result = HttpClientUtil.doPost(STOP_YUNTAI_CON, param);
		Map<String, String> map = JSONObject.parseObject(result, Map.class);
		if (map.get("code").equals("200")) {
			return true;
		}
		return false;
	}

}
